package com.sisgesbyte.dao.seguridad;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.sisgesbyte.dao.beanDao.FiltrosSQLBean;
import com.sisgesbyte.dao.beanDao.TablaDetRequest;
import com.sisgesbyte.dao.utilDao.CreaFiltroSqlString;
import com.sisgesbyte.data.entity.comun.GenericEntity;
import com.sisgesbyte.data.entity.seguridad.TablasListaEntity;
import com.sisgesbyte.data.util.AbstractFacade;

@Stateless
public class MantenimientoTablaDaoImpl extends
		AbstractFacade<TablasListaEntity> implements MantenimientoTablaDao {
	
	@Inject
	private CreaFiltroSqlString creaFiltroSqlString;
	
	@PersistenceContext(unitName = "SigesSiscompPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}

	public MantenimientoTablaDaoImpl() {
		super(TablasListaEntity.class);
	}

	@Override
	public List<TablasListaEntity> listarTablas(FiltrosSQLBean cadenaFiltros) {

		StringBuilder jpql = new StringBuilder();
		jpql.append("select ts.id, ts.nombre, ts.num_campos, ts.active from seguridad.tablas_sistema ts ");
		// concatenamos el filtro
		if (cadenaFiltros != null) {
			StringBuilder filtro = new StringBuilder();
			filtro.append(creaFiltroSqlString.concatenar(cadenaFiltros));
			jpql.append(filtro.toString());
		}
		Query query = em.createNativeQuery(jpql.toString(), TablasListaEntity.class);
		if (cadenaFiltros != null && cadenaFiltros.getListaValores() != null
				&& cadenaFiltros.getListaValores().size() > 0) {
			int num = 0;
			for (Object valor : cadenaFiltros.getListaValores()) {
				query.setParameter(++num, valor);
			}
		}
		List<TablasListaEntity> ls = query.getResultList();

		return ls;
	}

	@Override
	public GenericEntity guardarTablas(TablaDetRequest request) {
		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from seguridad.sp_registrar_tabla(:in_nombre, :in_numcampos, :in_active)");

		Query query = em
				.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_nombre", request.getNombreTabla());
		query.setParameter("in_numcampos", request.getNumCampos());
		query.setParameter("in_active", request.getActive());

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity actualizarTablas(TablaDetRequest request) {

		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from seguridad.sp_actualizar_tabla(:in_tablaid, :in_nombre, :in_numcampos, :in_active)");

		Query query = em
				.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_tablaid", request.getCodTabla());
		query.setParameter("in_nombre", request.getNombreTabla());
		query.setParameter("in_numcampos", request.getNumCampos());
		query.setParameter("in_active", request.getActive());

		return (GenericEntity) query.getSingleResult();
	}

	@Override
	public GenericEntity eliminarTabla(int codTabla) {

		StringBuilder jpql = new StringBuilder();
		jpql.append("select * from seguridad.sp_eliminar_tabla(:in_codtabla)");

		Query query = em
				.createNativeQuery(jpql.toString(), GenericEntity.class);
		query.setParameter("in_codtabla", codTabla);

		return (GenericEntity) query.getSingleResult();
	}

}
